<!DOCTYPE html>





<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="generator" content="Hexo 3.9.0">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.3.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.3.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.3.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '7.3.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    save_scroll: false,
    copycode: {"enable":false,"show_result":false,"style":null},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    }
  };
</script>

  <meta name="description" content="参考： http://bbs.elecfans.com/jishu_543262_1_1.html 配置：喇叭：3.5音频口 麦克风：摄像头自带麦克风（Usb） 1. 安装需要的软件12sudo apt-get install python-pip mplayer flac python2.7-dev libcurl4-gnutls-devsudo pip install requests pyc">
<meta name="keywords" content="树莓派">
<meta property="og:type" content="article">
<meta property="og:title" content="树莓派语音识别">
<meta property="og:url" content="http://yoursite.com/2018/02/01/树莓派语音识别/index.html">
<meta property="og:site_name" content="XuQi&#39;s Blog">
<meta property="og:description" content="参考： http://bbs.elecfans.com/jishu_543262_1_1.html 配置：喇叭：3.5音频口 麦克风：摄像头自带麦克风（Usb） 1. 安装需要的软件12sudo apt-get install python-pip mplayer flac python2.7-dev libcurl4-gnutls-devsudo pip install requests pyc">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-10-20T13:23:23.049Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="树莓派语音识别">
<meta name="twitter:description" content="参考： http://bbs.elecfans.com/jishu_543262_1_1.html 配置：喇叭：3.5音频口 麦克风：摄像头自带麦克风（Usb） 1. 安装需要的软件12sudo apt-get install python-pip mplayer flac python2.7-dev libcurl4-gnutls-devsudo pip install requests pyc">
  <link rel="canonical" href="http://yoursite.com/2018/02/01/树莓派语音识别/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>树莓派语音识别 | XuQi's Blog</title>
  








  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  <div class="container sidebar-position-left">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">XuQi's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
  </ul>

    

</nav>
</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content page-post-detail">
            

  <div id="posts" class="posts-expand">
    

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/02/01/树莓派语音识别/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="XuQi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="XuQi's Blog">
    </span>
      <header class="post-header">

        
          <h1 class="post-title" itemprop="name headline">树莓派语音识别

            
          </h1>
        

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2018-02-01 20:42:08" itemprop="dateCreated datePublished" datetime="2018-02-01T20:42:08+00:00">2018-02-01</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-10-20 13:23:23" itemprop="dateModified" datetime="2019-10-20T13:23:23+00:00">2019-10-20</time>
              </span>
            
          

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p><strong>参考：</strong></p>
<p><a href>http://bbs.elecfans.com/jishu_543262_1_1.html</a></p>
<h3 id="配置："><a href="#配置：" class="headerlink" title="配置："></a>配置：</h3><p>喇叭：3.5音频口</p>
<p>麦克风：摄像头自带麦克风（Usb）</p>
<h4 id="1-安装需要的软件"><a href="#1-安装需要的软件" class="headerlink" title="1. 安装需要的软件"></a>1. 安装需要的软件</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get install python-pip mplayer flac python2.7-dev libcurl4-gnutls-dev</span><br><span class="line">sudo pip install requests pycurl</span><br></pre></td></tr></table></figure>

<a id="more"></a>

<h4 id="2-配置配置麦克风和音箱"><a href="#2-配置配置麦克风和音箱" class="headerlink" title="2. 配置配置麦克风和音箱"></a>2. 配置配置麦克风和音箱</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">$ cat /proc/asound/cards</span><br><span class="line"></span><br><span class="line">0 [ALSA           ]: bcm2835 - bcm2835 ALSA</span><br><span class="line">                      bcm2835 ALSA</span><br><span class="line">1 [Camera         ]: USB-Audio - USB 2.0 Camera</span><br><span class="line">                      Sonix Technology Co., Ltd. USB 2.0 Camera at usb-3f980000.usb-1.2, high speed</span><br><span class="line">                      </span><br><span class="line">$ cat /proc/asound/modules</span><br><span class="line"> 0 snd_bcm2835</span><br><span class="line"> 1 snd_usb_audio</span><br></pre></td></tr></table></figure>

<p>第一个命令查看声卡是否存在</p>
<p>第二个命令应该是看声卡的驱动的</p>
<p>调整输入输出音频工具alsamixer：</p>
<blockquote>
<p>sudo alsamixer</p>
</blockquote>
<h4 id="3-播放声音"><a href="#3-播放声音" class="headerlink" title="3. 播放声音"></a>3. 播放声音</h4><blockquote>
<p>aplay make.wav</p>
</blockquote>
<p>结果声音没有播放</p>
<p>网上查了，</p>
<p>By default output will be automatic (hdmi if hdmi supports audio, otherwise analogue). You can force it with:</p>
<blockquote>
<p>amixer cset numid=3 <n><br>where n is 0=auto, 1=headphones, 2=hdmi.</n></p>
</blockquote>
<p>If you have pulseaudio installed you need to also specify the card number:</p>
<blockquote>
<p>amixer -c 0 cset numid=3 <n></n></p>
</blockquote>
<p><a href>http://elinux.org/R-Pi_Troubleshooting#Sound_does_not_work_at_all.2C_or_in_some_applications</a></p>
<p>所以运行</p>
<blockquote>
<p>amixer cset numid=3 1<br>就可以听到3.5输出了</p>
</blockquote>
<h4 id="4-麦克风测试"><a href="#4-麦克风测试" class="headerlink" title="4. 麦克风测试"></a>4. 麦克风测试</h4><blockquote>
<p>sudo arecord –duration=10 –device=plughw:1,0 –format=cd aaa.wav</p>
</blockquote>
<p>指令中的’–device=plughw:1,0’ 选项指派特定的录音设备，而Raspberry Pi的内置音效设备是’plughw:0,0’ </p>
<p>使用百度api进行语音播放：</p>
<h4 id="5-使用百度API进行语音识别和播放"><a href="#5-使用百度API进行语音识别和播放" class="headerlink" title="5. 使用百度API进行语音识别和播放"></a>5. 使用百度API进行语音识别和播放</h4><p><a href>http://yuyin.baidu.com/docs/tts/135</a></p>
<p>测试取的token：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">import requests</span><br><span class="line">import json</span><br><span class="line">url = &apos;https://openapi.baidu.com/oauth/2.0/token&apos;</span><br><span class="line"></span><br><span class="line">payload = &#123;&apos;grant_type&apos;:&apos;client_credentials&apos;,</span><br><span class="line">           &apos;client_id&apos;:&apos;ZEzwlE2gipnqoE3jqiCKN7SN&apos;,</span><br><span class="line">           &apos;client_secret&apos;:&apos;KOyWkyE57qWnDnFoc5afwIk0zBMw2X1p&apos;&#125;</span><br><span class="line"></span><br><span class="line">r = requests.post(url,data=payload)</span><br><span class="line"></span><br><span class="line">print json.loads(r.text)[&apos;access_token&apos;]</span><br></pre></td></tr></table></figure>

<p>测试ok后，开始正式写：</p>
<p><a href="https://github.com/xuqi1987/10.voice" target="_blank" rel="noopener">https://github.com/xuqi1987/10.voice</a></p>
<p>大致思路：<br>将请求的token保存到文件中，然后读取文件中的accesstoken,判断token的日期是否有效，无效重新请求token。</p>
<p>代码：<br>app.py</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br></pre></td><td class="code"><pre><span class="line"># -*- coding:utf8 -*-</span><br><span class="line">import requests</span><br><span class="line">import os</span><br><span class="line">import json</span><br><span class="line">import time</span><br><span class="line">import uuid</span><br><span class="line">import base64</span><br><span class="line"></span><br><span class="line">class API(object):</span><br><span class="line"></span><br><span class="line">    def __init__(self):</span><br><span class="line">        dic = &#123;</span><br><span class="line">            &apos;hello&apos;:&apos;欢迎使用语音播放&apos;,</span><br><span class="line">            &apos;error&apos;:&apos;识别错误&apos;,</span><br><span class="line">        &#125;</span><br><span class="line">        for key in dic:</span><br><span class="line">            self.txt2audio(dic[key],key)</span><br><span class="line">            if key == &apos;hello&apos;:</span><br><span class="line">                self.play(key)</span><br><span class="line">        pass</span><br><span class="line"></span><br><span class="line">    def get_token(self):</span><br><span class="line"></span><br><span class="line">        token = None</span><br><span class="line">        if os.path.exists(&apos;token&apos;):</span><br><span class="line">            file = open(&apos;token&apos;,&apos;r&apos;)</span><br><span class="line">            token = file.read()</span><br><span class="line">            file.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">            if json.loads(token)[&apos;expires&apos;] &lt; time.time():</span><br><span class="line">                token = self.req_token()</span><br><span class="line">            else:</span><br><span class="line">                print &quot;load cache token&quot;</span><br><span class="line">            pass</span><br><span class="line">        else:</span><br><span class="line">            token = self.req_token()</span><br><span class="line"></span><br><span class="line">        return json.loads(token)[&apos;access_token&apos;]</span><br><span class="line"></span><br><span class="line">    def req_token(self):</span><br><span class="line">        print &quot;request new token&quot;</span><br><span class="line">        token = None</span><br><span class="line">        url = &apos;https://openapi.baidu.com/oauth/2.0/token&apos;</span><br><span class="line">        key = &apos;ZEzwlE2gipnqoE3jqiCKN7SN&apos;</span><br><span class="line">        secret = &apos;KOyWkyE57qWnDnFoc5afwIk0zBMw2X1p&apos;</span><br><span class="line">        payload = &#123;&apos;grant_type&apos;:&apos;client_credentials&apos;,</span><br><span class="line">                   &apos;client_id&apos;:key,</span><br><span class="line">                   &apos;client_secret&apos;:secret&#125;</span><br><span class="line"></span><br><span class="line">        r = requests.post(url,data=payload)</span><br><span class="line">        token = r.text</span><br><span class="line">        jtoken = json.loads(token)</span><br><span class="line">        jtoken[&apos;expires&apos;] = jtoken[&apos;expires_in&apos;] + time.time()</span><br><span class="line">        token = json.dumps(jtoken)</span><br><span class="line"></span><br><span class="line">        file = open(&apos;token&apos;,&apos;w&apos;)</span><br><span class="line">        file.write(token)</span><br><span class="line">        file.close()</span><br><span class="line">        return token</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    def txt2audio(self,txt,audio=&apos;tmp&apos;):</span><br><span class="line">        access_token = self.get_token()</span><br><span class="line">        url = &quot;http://tsn.baidu.com/text2audio&quot;</span><br><span class="line">        payload = &#123;</span><br><span class="line">            &apos;tex&apos;:txt,</span><br><span class="line">            &apos;lan&apos;:&apos;zh&apos;,</span><br><span class="line">            &apos;tok&apos;:access_token,</span><br><span class="line">            &apos;ctp&apos;:1,</span><br><span class="line">            &apos;cuid&apos;:uuid.UUID(int = uuid.getnode()).hex,</span><br><span class="line">            &apos;spd&apos;:5,</span><br><span class="line">            &apos;pit&apos;:8,</span><br><span class="line">            &apos;vol&apos;:9,</span><br><span class="line">            &apos;per&apos;:1,</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        r = requests.post(url,payload)</span><br><span class="line"></span><br><span class="line">        if r.headers[&apos;Content-Type&apos;] == &apos;audio/mp3&apos;:</span><br><span class="line">            file = open(audio + &apos;.mp3&apos;,&apos;w&apos;)</span><br><span class="line">            file.write(r.content)</span><br><span class="line">            file.close()</span><br><span class="line"></span><br><span class="line">        else:</span><br><span class="line">            self.play(&apos;error&apos;)</span><br><span class="line">            print r.text</span><br><span class="line">        pass</span><br><span class="line"></span><br><span class="line">    def audio2txt(self,file=&apos;rec&apos;):</span><br><span class="line">        access_token = self.get_token()</span><br><span class="line">        url=&quot;http://vop.baidu.com/server_api&quot;</span><br><span class="line"></span><br><span class="line">        self.txt2audio(&apos;请说:&apos;)</span><br><span class="line">        self.play(&apos;tmp&apos;)</span><br><span class="line"></span><br><span class="line">        os.system(&apos;arecord --duration=2 -c 1 --device=plughw:1,0 -f S16_LE -t wav -v  --rate=16000 &apos; + file + &apos;.wav&apos;)</span><br><span class="line"></span><br><span class="line">        f = open(file + &apos;.wav&apos;,&apos;r&apos;)</span><br><span class="line">        sound = f.read()</span><br><span class="line">        f.close()</span><br><span class="line"></span><br><span class="line">        payload = &#123;</span><br><span class="line">        &apos;format&apos;:&apos;wav&apos;,</span><br><span class="line">        &apos;rate&apos;:16000,</span><br><span class="line">        &apos;channel&apos;:1,</span><br><span class="line">        &apos;cuid&apos;:uuid.UUID(int = uuid.getnode()).hex,</span><br><span class="line">        &apos;token&apos;:access_token,</span><br><span class="line">        &apos;lan&apos;:&apos;zh&apos;,</span><br><span class="line">        &apos;speech&apos;:base64.b64encode(sound),</span><br><span class="line">        &apos;len&apos;:len(sound),</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        header = &#123;&apos;Content-Type&apos;:&apos;application/json&apos;&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        r = requests.post(url,data=json.dumps(payload),headers=header)</span><br><span class="line">        print r.text</span><br><span class="line">	jret = json.loads(r.text)</span><br><span class="line">	if jret.has_key(&apos;result&apos;):</span><br><span class="line">        	ret = json.loads(r.text)[&apos;result&apos;][0].split(&apos;,&apos;)[0]</span><br><span class="line">        else:</span><br><span class="line">		ret = &quot;&quot;</span><br><span class="line">	return ret</span><br><span class="line">        pass</span><br><span class="line"></span><br><span class="line">    def play(self,file=&apos;tmp&apos;):</span><br><span class="line">        # can not play the mp3 by aplay</span><br><span class="line">        # os.system(&quot;aplay &quot; + file + &quot;.mp3&quot;)</span><br><span class="line">        os.system(&quot;mpg321 &quot; + file + &quot;.mp3&quot;)</span><br><span class="line">        pass</span><br></pre></td></tr></table></figure>

<p>baidu.py</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"># -*- coding:utf8 -*-</span><br><span class="line">from baidu import API</span><br><span class="line">import RPi.GPIO as GPIO</span><br><span class="line">import time</span><br><span class="line">import requests</span><br><span class="line">import uuid</span><br><span class="line">import json</span><br><span class="line">pin = 13</span><br><span class="line"></span><br><span class="line">GPIO.setmode(GPIO.BCM)</span><br><span class="line">GPIO.setup(pin, GPIO.IN)</span><br><span class="line"></span><br><span class="line">baidu = API()</span><br><span class="line"># baidu.txt2audio(&quot;你好&quot;)</span><br><span class="line"># baidu.play(&apos;tmp&apos;)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def get_tuling(text):</span><br><span class="line"></span><br><span class="line">    url=&apos;http://www.tuling123.com/openapi/api&apos;</span><br><span class="line">    data=&#123;&apos;key&apos;:&apos;fa78fe2fbb85c914c7126d42bc7c3ebb&apos;,&apos;info&apos;:text,&apos;userid&apos;:str(uuid.UUID(int = uuid.getnode()).hex,)&#125;</span><br><span class="line">    r = requests.post(url,data=data)</span><br><span class="line">    ans = json.loads(r.text)</span><br><span class="line">    return ans</span><br><span class="line"></span><br><span class="line">while True:</span><br><span class="line"></span><br><span class="line">    change = False</span><br><span class="line"></span><br><span class="line">    while GPIO.input(pin) == GPIO.LOW:</span><br><span class="line">        change = True</span><br><span class="line">        time.sleep(0.005)</span><br><span class="line"></span><br><span class="line">    if change == True:</span><br><span class="line">        #baidu.txt2audio(&apos;我听着,请说指令:&apos;)</span><br><span class="line">        text = baidu.audio2txt()</span><br><span class="line">        print text</span><br><span class="line">	if len(text) &gt; 0:</span><br><span class="line">        	rep = get_tuling(text)</span><br><span class="line">        	rep = rep[&apos;text&apos;]</span><br><span class="line">		print rep</span><br><span class="line">	else:</span><br><span class="line">		rep = &quot;我没有听清&quot;</span><br><span class="line">	baidu.txt2audio(rep)</span><br><span class="line">	baidu.play()	</span><br><span class="line">	change = False</span><br><span class="line">	</span><br><span class="line">    time.sleep(1)</span><br></pre></td></tr></table></figure>

<p><strong>遇到坑：</strong></p>
<ol>
<li>mp3格式只能通过mpg321播放</li>
<li>读取文件token = file.readlines()[0]，是一个列表，所以需要先取第一个再用json转。</li>
</ol>

    </div>

    
    
    
        
      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/树莓派/" rel="tag"># 树莓派</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/2018/02/01/网络Tcp-reset消息/" rel="next" title="TCP reset及原因">
                  <i class="fa fa-chevron-left"></i> TCP reset及原因
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/2018/02/01/树莓派Html 视频直播/" rel="prev" title="Html视频直播">
                  Html视频直播 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">

          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">XuQi</p>
  <div class="site-description motion-element" itemprop="description"></div>
</div>
  <nav class="site-state motion-element">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">44</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
        
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
        <span class="site-state-item-count">30</span>
        <span class="site-state-item-name">标签</span>
        
      </div>
    
  </nav>



        </div>
      </div>
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#配置："><span class="nav-number">1.</span> <span class="nav-text">配置：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-安装需要的软件"><span class="nav-number">1.1.</span> <span class="nav-text">1. 安装需要的软件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-配置配置麦克风和音箱"><span class="nav-number">1.2.</span> <span class="nav-text">2. 配置配置麦克风和音箱</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-播放声音"><span class="nav-number">1.3.</span> <span class="nav-text">3. 播放声音</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-麦克风测试"><span class="nav-number">1.4.</span> <span class="nav-text">4. 麦克风测试</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-使用百度API进行语音识别和播放"><span class="nav-number">1.5.</span> <span class="nav-text">5. 使用百度API进行语音识别和播放</span></a></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">XuQi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.9.0</div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v7.3.0</div>

        








        
      </div>
    </footer>
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
      </div>

    

  </div>

  
  <script src="/lib/jquery/index.js?v=3.4.1"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

<script src="/js/utils.js?v=7.3.0"></script>
  <script src="/js/motion.js?v=7.3.0"></script>


  <script src="/js/schemes/muse.js?v=7.3.0"></script>


<script src="/js/next-boot.js?v=7.3.0"></script>




  




























  

  

  


  
  <script src="/js/scrollspy.js?v=7.3.0"></script>
<script src="/js/post-details.js?v=7.3.0"></script>


</body>
</html>
